﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnllyPage4UIController : MonoBehaviour {

    private int[,] arr = new int[9, 3];
    private int i;
    private int j;
    public Transform tf_page4;

    // Use this for initialization
    void Start()
    {
        EventManager<string>.AddEvent(MyEventType.OnllyDirButtonClickEvent, OnDirButtonClick);
        EventManager<string>.AddEvent(MyEventType.XuanzhuanAnniuEvent, OnXuanzhuanAnniu);
        EventManager<bool>.AddEvent(MyEventType.OnllyReSetPageEvent, ReSet);
        Init();
    }

    void Init() {
        for (int i = 0; i < 9; i++) {
            arr[i, 0] = 2;
        }
    }

    private void OnXuanzhuanAnniu(string name)
    {
        if (OnllyUIController._instance.i_pageNum == 3 && !OnllyUIController._instance.b_isDuihuakuang)
        {
            switch (name)
            {
                case "shun":
                    OnRightButtonClick(true); break;
                case "ni":
                    OnDownButtonClick(true); break;
                default: break;
            }
        }
    }

    private void OnDirButtonClick(string name)
    {
        if (OnllyUIController._instance.i_pageNum == 3 && !OnllyUIController._instance.b_isDuihuakuang)
        {
            switch (name)
            {
                case "up":
                    OnUpButtonClick(true); break;
                case "down":
                    OnDownButtonClick(true); break;
                case "left":
                    OnLeftButtonClick(true); break;
                case "right":
                    OnRightButtonClick(true); break;
                default: break;
            }
        }
    }

    private void OnRightButtonClick(bool v)
    {
        string name = i + "_" + j;
        OnllyUIController._instance.ChangeType(tf_page4, 0, name);
        j++;
        if ((i == 7 && j == 2) || (i == 8 && j == 2)) {

        } else if (j>=2) {
            j = 0;
        }
        name = i + "_" + j;
        if(arr[i,j]==2)
            OnllyUIController._instance.ChangeType(tf_page4, 4, name);
        else
            OnllyUIController._instance.ChangeType(tf_page4, 5, name);
    }

    private void OnLeftButtonClick(bool v)
    {
        string name = i + "_" + j;
        OnllyUIController._instance.ChangeType(tf_page4, 0, name);
        j--;
        if ((i == 7 && j < 0) || (i == 8 && j <0))
        {
            j = 2;
        }
        else if (j <0)
        {
            j = 1;
        }
        name = i + "_" + j;
        if (arr[i, j] == 2)
            OnllyUIController._instance.ChangeType(tf_page4, 4, name);
        else
            OnllyUIController._instance.ChangeType(tf_page4, 5, name);
    }

    private void OnDownButtonClick(bool v)
    {
        string name = i + "_" + j;
        OnllyUIController._instance.ChangeType(tf_page4, 0, name);
        i++;
        if (i == 9 && j == 2)
        {
            i = 7;
        }
        else if (i>8)
        {
            i = 0;
        }
        name = i + "_" + j;
        if (arr[i, j] == 2)
            OnllyUIController._instance.ChangeType(tf_page4, 4, name);
        else
            OnllyUIController._instance.ChangeType(tf_page4, 5, name);
    }

    private void OnUpButtonClick(bool v)
    {
        string name = i + "_" + j;
        OnllyUIController._instance.ChangeType(tf_page4, 0, name);
        i--;
        if (i == 6 && j == 2)
        {
            i = 8;
        }
        else if (i < 0)
        {
            i = 8;
        }
        name = i + "_" + j;
        if (arr[i, j] == 2)
            OnllyUIController._instance.ChangeType(tf_page4, 4, name);
        else
            OnllyUIController._instance.ChangeType(tf_page4, 5, name);
    }

    private void ReSet(bool obj)
    {
        if (OnllyUIController._instance.i_pageNum == 3)
        {
            string name = i + "_" + j;
            OnllyUIController._instance.ChangeType(tf_page4, 0, name);
            i = 0;
            j = 0;
            name = i + "_" + j;
            if (arr[i, j] == 2)
                OnllyUIController._instance.ChangeType(tf_page4, 4, name);
            else
                OnllyUIController._instance.ChangeType(tf_page4, 5, name);
        }
    }
}
